from datetime import datetime, timedelta
import time
import jwt
import hashlib
import logging

logger = logging.getLogger('AuthAPI')

def validate_timestamp(timestamp):
    try:
        server_time = int(time.time())
        client_time = int(timestamp)
        return abs(server_time - client_time) <= 300
    except Exception as e:
        logger.error(f"时间戳解析失败: {str(e)}")
        return False

def generate_signature(ticket, device_id, secret_key):
    try:
        sign_str = f"{ticket}{device_id}{secret_key}"
        return hashlib.md5(sign_str.encode('utf-8')).hexdigest().lower()
    except Exception as e:
        logger.error(f"签名生成失败: {traceback.format_exc()}")
        return ""